import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
import Reg from '@/views/Reg/Reg.vue'
import Login from '@/views/Login/Login'
import Main from '@/views/Main/Main'
import Home from '@/views/Menus/Home/Home'
import UserAvatar from '@/views/Menus/User/UserAvatar'
import UserInfo from '@/views/Menus/User/UserInfo'
import UserPwd from '@/views/Menus/User/UserPwd'
import ArtCate from '@/views/Article/ArtCate'
import ArtList from '@/views/Article/ArtList'
import ArtAdd from '@/views/Article/ArtAdd'

Vue.use(VueRouter)



const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
const routes = [
  {
    path: '*',
    redirect: '/login'
  },
  {
    path: '/reg',
    name: 'reg',
    component: Reg
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/',
    name: 'Main',
    component: Main,
    redirect: '/home',
    children: [
      { path: 'home', component: Home },
      { path: 'user-avatar', component: UserAvatar },
      { path: 'user-info', component: UserInfo },
      { path: 'user-pwd', component: UserPwd },
      { path: 'art-cate', component: ArtCate },
      { path: 'art-list', component: ArtList },
      { path: 'art-add', component: ArtAdd },
    ]
  },
]

const router = new VueRouter({
  routes
})

// console.log(token == false && !(to.path == '/login' || to.path == '/reg'));
router.beforeEach((to, from, next) => {
  const token = store.state.token
  if (!token && to.path !== '/login' && to.path !== '/reg') {
    next('/login')
  } else {
    next()
  }
})

export default router
